#!/usr/bin/python
# -*- coding: cp1250 -*-

from pepe.glib import *
import pepe.p_utils.txt2pg as ppt

def main(filemask):
    print '''
    load table to pg'''
    print '''    filemask = ''', filemask
    for f in sorted(glob.glob(filemask)):

        print '    ', timenow(), f
        p = ppt.TxtLoad()
        p.initFile(  inputFile=f
                   , colDelimiter='\t'
                   , hasHeader=1
                   )

        p.pgClientEncoding = 'UTF-8'

        if 1: # scan file and create string
            p.queryCreateTable()
            #of = open('#create-and-load.sql', 'a'); of.write(p.queryCreateString); of.close()

        if 1: # (modify query), create table and load data
            #p.queryCreateString = open('#c.sql', 'r').read()
            
            p.queryLoadTable()
            #of = open('#l.sql', 'w'); of.write(p.queryLoadString); of.close()
            
            p.createTable()
            p.loadData()
        print '    ', timenow()

if __name__ == "__main__":
    """ """
    if len(sys.argv) > 1:
        main(sys.argv[1])
    else:
        print 'Wrong command syntax (argument is missing). Try again now.'
